package com.tensquare.jms.consumer.listener;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import util.SmsUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by WF on 2020/10/23 10:42
 * 获取到发来的验证码消息，并同时向阿里大于发送消息
 */
@RabbitListener(queues = "sms")
@Component
public class SmsListener {

    @Autowired
    private SmsUtil smsUtil;
    @RabbitHandler
    public void getMessage(Map map){
        try {
            System.out.println("map = " + map);

            //1.获取发来的消息
            String mobile = (String) map.get("mobile");   //手机号
            Integer  code = (Integer) map.get("code");      //验证码
            String templateCode = (String) map.get("templateCode"); //模板code
            String signName = (String) map.get("signName"); //签名

            //2.重新组合code为json串
            Map codeMap = new HashMap();
            codeMap.put("code",code+"");
            String param = JSON.toJSONString(codeMap);

            //3.向阿里大于发送消息
            smsUtil.sendSms(mobile,templateCode,signName,param);

        } catch (ClientException e) {
            e.printStackTrace();
        }

    }
}
